package cn.x.functions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import cn.x.utils.RandomIdcard;
import org.apache.jmeter.engine.util.CompoundVariable;
import org.apache.jmeter.functions.AbstractFunction;
import org.apache.jmeter.functions.InvalidVariableException;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.samplers.Sampler;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author a tester
 * @date 2021/5/22
 */
public class AIdCardNo extends AbstractFunction {

    private static final Logger log = LoggerFactory.getLogger(AIdCardNo.class);

    private static final List<String> desc = new ArrayList<>();

    private static final String KEY = "__AIdCardNo"; //$NON-NLS-1$

    /**
     * 参数一：地区码，如31，42
     * 参数二：最早出生年
     * 参数三：最晚出生年
     * 参数三：存储的变量名
     */
    static {
        desc.add("地区码，如31，42"); //$NON-NLS-1$
        desc.add("最早出生年"); //$NON-NLS-1$
        desc.add("最晚出生年"); //$NON-NLS-1$
        desc.add(JMeterUtils.getResString("function_name_paropt")); //$NON-NLS-1$
    }


    private static final int MAX_PARAM_COUNT = 4;

    private CompoundVariable[] values;

    private static final int MIN_PARAM_COUNT = 0;

    private static final int AREA_CODE = 1;

    private static final int MIN_YEAR = 2;

    private static final int MAX_YEAR = 3;

    private static final int PARAM_NAME = 4;


    /**
     * No-arg constructor.
     */
    public AIdCardNo() {
        super();
    }

    /** {@inheritDoc} */
    @Override
    public String execute(SampleResult previousResult, Sampler currentSampler)
            throws InvalidVariableException {

        String areaCodePrefix = "";//means no restriction
        if (values.length >= AREA_CODE) {
            areaCodePrefix = values[AREA_CODE - 1].execute().trim();
        }

        String minYear = "";//means no restriction
        if (values.length >= MIN_YEAR) {
            minYear = values[MIN_YEAR - 1].execute().trim();
        }

        String maxYear = "";//means no restriction
        if (values.length >= MAX_YEAR) {
            maxYear = values[MAX_YEAR - 1].execute().trim();
        }

        String myName = "";//$NON-NLS-1$
        if (values.length >= PARAM_NAME) {
            myName = values[PARAM_NAME - 1].execute().trim();
        }

        String myValue = RandomIdcard.getIdentity(areaCodePrefix,minYear,maxYear);

        if (myName.length() > 0) {
            JMeterVariables vars = getVariables();
            if (vars != null) {// Can be null if called from Config item testEnded() method
                vars.put(myName, myValue);
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("{} name:{} value:{}", Thread.currentThread().getName(), myName, myValue); //$NON-NLS-1$
        }

        return myValue;
    }

    /** {@inheritDoc} */
    @Override
    public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
        checkParameterCount(parameters, MIN_PARAM_COUNT, MAX_PARAM_COUNT);
        values = parameters.toArray(new CompoundVariable[parameters.size()]);
    }

    /** {@inheritDoc} */
    @Override
    public String getReferenceKey() {
        return KEY;
    }

    /** {@inheritDoc} */
    @Override
    public List<String> getArgumentDesc() {
        return desc;
    }
}
